import 'package:flutter/material.dart';
import 'package:flutter_app/model/news.dart';
import 'package:flutter_app/style/color.dart';
import 'package:flutter_app/view/news/news_details.dart';
import 'package:flutter_app/view/photoViewGalleryScreen.dart';
import 'package:flutter_app/tools/utils.dart';

// ignore: must_be_immutable
class NewsGridItem extends StatefulWidget {
  final News news;
  NewsGridItem({@required this.news, Key key}) : super(key: key);

  @override
  NewsGridItemState createState() => NewsGridItemState();
}

// ignore: camel_case_types
class NewsGridItemState extends State<NewsGridItem> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //跳转到新闻详情页
    Function onTapped = (BuildContext context) {
      Navigator.push(context, new MaterialPageRoute(builder: (context) {
        //TODO:
        return new NewsDetails(widget.news, key: UniqueKey());
      }));
    };

    return Card(
      color: Colors.white,
      child: Center(
        child: InkWell(
            child: Container(
                height: 148,
                child: Column(
                  children: [
                    Row(
                      children: [
                        //新闻来源
                        Container(
                          padding: EdgeInsets.all(5.0),
                          child: new Text(
                            widget.news.source,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 24.0,
                              height: 1.2,
                            ),
                          ),
                        ),
                        //发布时间
                        Container(
                            margin: EdgeInsets.fromLTRB(5, 0, 0, 0),
                            padding: EdgeInsets.all(.0),
                            child: new Text(
                              m_utils.timeDifferenceToNow(
                                  widget.news.inTime, DateTime.now()),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                color: m_color.grey,
                                fontSize: 12.0,
                                height: 1.2,
                              ),
                            )),
                      ],
                    ),
                    Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          //新闻标题
                          Expanded(
                            child: Padding(
                                padding: EdgeInsets.all(.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      margin: widget.news.title.length == 0
                                          ? EdgeInsets.all(0)
                                          : EdgeInsets.fromLTRB(
                                              15.0, 10.0, 15.0, 10.0),
                                      alignment: Alignment.centerLeft,
                                      child: widget.news.title.length == 0
                                          ? null
                                          : Text(
                                              widget.news.title,
                                              overflow: TextOverflow.ellipsis,
                                              maxLines: 3,
                                              style: TextStyle(fontSize: 18),
                                            ),
                                    ),
                                  ],
                                )),
                          ),

                          //图片区域
                          widget.news.image != ""
                              ? Container(
                                  margin: EdgeInsets.all(10),
                                  decoration:
                                      BoxDecoration(color: Colors.white),
                                  width: 120,
                                  height: 75,
                                  child: buildImageView(context, widget.news),
                                )
                              : Container(),
                        ]),
                  ],
                )),
            onTap: () {
              onTapped(context);
            }),
      ),
    );
  }
}

Widget buildImageView(BuildContext context, News news) {
  if (news.image != "") {
    return Image.network(news.image, fit: BoxFit.scaleDown);
  } else {
    return null;
  }
}

//点击图片查看高清图片。
viewPhoto(BuildContext context, List<String> images, List<String> thumbnails,
    int index) {
  //FadeRoute是自定义的切换过度动画（渐隐渐现） 如果不需要 可以使用默认的MaterialPageRoute
  Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
    return PhotoViewGalleryScreen(
      images: images, //传入图片list
      thumbnails: thumbnails,
      heroTag: images[index],
      index: index, //传入当前点击的图片的index
    );
  }));
}

Widget buildGifLogo() {
  return Container(
    padding: EdgeInsets.fromLTRB(7, 0, 7, 2),
    decoration: BoxDecoration(
      color: Colors.black54,
      border: Border.all(color: Colors.white, width: 0.8),
      borderRadius: BorderRadius.all(Radius.circular(3)), // 也可控件一边圆角大小
    ),
    child: Text(
      "动图",
      style: TextStyle(color: Colors.white, fontSize: 12),
    ),
  );
}
